% analyze and plot data gathered while playing dotris
gameLogic = dotrisLogic;
nTypes = length(gameLogic.pieceNames);

% pull data from the log
log = topsDataLog.theDataLog;
scoreLog = log.getAllItemsFromGroupAsStruct('score');
score = [scoreLog.item];
scoreGain = [0, diff(score)];

%%
if isempty(score) || score == 0
    disp('no lines to analyze')
    return
end

pieceTypeLog = log.getAllItemsFromGroupAsStruct('piece type');
pieceType = [pieceTypeLog.item];

nextPieceTypeLog = log.getAllItemsFromGroupAsStruct('next piece type');
nextType = [nextPieceTypeLog.item];

nPieces = length(score);

pairCount = zeros(nTypes, nTypes);
pairValue = zeros(nTypes, nTypes);
deferredValue = zeros(nTypes, nTypes);
for ii = 1:(nPieces-1)
    now = pieceType(ii);
    next = nextType(ii);
    pairCount(next, now) = pairCount(next, now) + 1;
    pairValue(next, now) = pairValue(next, now) + scoreGain(ii);
    deferredValue(next, now) = ...
        deferredValue(next, now) + (scoreGain(ii+1) > scoreGain(ii));
end

% occurrence of piece types and pairs
typeCount = sum(pairCount,1);
typeFrequency = typeCount ./ nPieces;
pairFrequency = pairCount ./ nPieces;
pairFrequencyNormalized = pairCount ./ max(pairCount(1:end));

% score value of piece types and pairs
typeValue = sum(pairValue, 1);
typeRelativeValue = typeValue ./ score(end);
pairRelativeValue = pairValue ./ pairFrequency;
pairValueNormalized = pairRelativeValue ./ max(pairRelativeValue(1:end));

% score value for waiting until next piece
deferredValueNormalized = deferredValue ./ max(deferredValue(1:end));

f = figure(65);
clf(f);
set(f, ...
    'MenuBar', 'none', ...
    'ToolBar', 'none', ...
    'NumberTitle', 'off', ...
    'Name', 'dotris summary');
types = 1:nTypes;
nColors = 100;
map = winter(nColors);

% show score growth
ax = subplot(2,2,1, ...
    'Parent', f, ...
    'YLim', [0 score(end)], ...
    'YTick', [0 score(end)], ...
    'XLim', [0, nPieces]);
line(1:nPieces, score, ...
    'Parent', ax, ...
    'Marker', '.', ...
    'MarkerSize', 5, ...
    'LineStyle', 'none', ...
    'Color', [0 .75 0])
xlabel(ax, '# of pieces')
ylabel(ax, '# of lines')
title(ax, 'score')

% show frequency and value of each piece type
ax = subplot(2,2,2, ...
    'Parent', f, ...
    'YLim', [0 1], ...
    'XLim', [0, nTypes+1], ...
    'XTick', types, ...
    'XTickLabel', gameLogic.pieceNames);
line(1:nTypes, typeFrequency, ...
    'Parent', ax, ...
    'Marker', '.', ...
    'LineStyle', 'none', ...
    'Color', [1 0 0])
line(1:nTypes, typeRelativeValue, ...
    'Parent', ax, ...
    'Marker', '*', ...
    'LineStyle', 'none', ...
    'Color', [0 0 1])
legend(ax, 'type frequency', 'type value', ...
    'Location', 'northwest')
title(ax, 'piece types')

% show effect of next piece on immediate score
ax = subplot(2,2,4, ...
    'Parent', f, ...
    'YLim', [0, nTypes+1], ...
    'YTick', types, ...
    'YTickLabel', gameLogic.pieceNames, ...
    'XLim', [0, nTypes+1], ...
    'XTick', types, ...
    'XTickLabel', gameLogic.pieceNames);
colormap(ax, map);
image( ...
    'Parent', ax, ...
    'XData', types, ...
    'YData', types, ...
    'CData', pairValueNormalized*nColors);
xlabel(ax, 'current piece')
title(ax, 'value of pair')

% show effect of next piece on deferred score
ax = subplot(2,2,3, ...
    'Parent', f, ...
    'YLim', [0, nTypes+1], ...
    'YTick', types, ...
    'YTickLabel', gameLogic.pieceNames, ...
    'XLim', [0, nTypes+1], ...
    'XTick', types, ...
    'XTickLabel', gameLogic.pieceNames);
colormap(ax, map);
image( ...
    'Parent', ax, ...
    'XData', types, ...
    'YData', types, ...
    'CData', deferredValueNormalized*nColors);
xlabel(ax, 'current piece')
ylabel(ax, 'next piece')
title(ax, 'value of using next')
